package com.qf.controller;

import com.qf.pojo.Address;
import com.qf.pojo.User;
import com.qf.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/address")
public class AddressController {

    @Autowired
    AddressService addressService;

    //获取地址
    @RequestMapping("/addAddress")
    public String addAddress(HttpServletRequest req){
        //获取地址参数
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String detail = req.getParameter("detail");

        //获取用户购物车信息
        User user = (User) req.getSession().getAttribute("user");
        int uid = user.getId();

        Address address = new Address();
        address.setUid(uid);
        address.setDetail(detail);
        address.setName(name);
        address.setPhone(phone);
        address.setLevel(0);
        addressService.addAddress(address);

        //响应前端
        return "redirect:/user?method=getAddress";

    }

    //添加地址
    @RequestMapping("/getAddress")
    public String getAddress(HttpServletRequest req){
        //获取用户
        User user = (User) req.getSession().getAttribute("user");

        List<Address> addressList = addressService.getAddressList(user.getId());

        //存储信息到 请求域
        req.setAttribute("addList",addressList);
        //响应前端
        return "forward:/self_info";
    }

    //更新地址
    @RequestMapping("/updateAddress")
    public String updateAddress(HttpServletRequest req){
        //获取地址参数
        int id = Integer.parseInt(req.getParameter("id"));
        String name = req.getParameter("name");
        String phone = req.getParameter("phone");
        String detail = req.getParameter("detail");

        //获取用户购物车信息
        User user = (User) req.getSession().getAttribute("user");
        int uid = user.getId();

        Address address = new Address();
        address.setId(id);
        address.setDetail(detail);
        address.setName(name);
        address.setPhone(phone);

        addressService.updateAddress(address);

        //响应前端
        return "redirect:/user?method=getAddress";
    }

    //删除
    @RequestMapping("/deleteAddress")
    public  String deleteAddress(HttpServletRequest req){
        //获取参数
        int id = Integer.parseInt(req.getParameter("id"));

        addressService.deleteAddress(id);

        return "redirect:/user?method=getAddress";
    }

    //设为默认
    @RequestMapping("/defaultAddress")
    public String defaultAddress(HttpServletRequest req){
        //获取参数id
        int aid = Integer.parseInt(req.getParameter("id"));
        //获取用户信息
        User user = (User) req.getSession().getAttribute("user");
        //调用service
        addressService.defaultAddress(user.getId(),aid);
        //响应前端
        return "redirect:/user?method=getAddress";
    }
}
